package com.fcwy.puzzle.view;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.viewpager.widget.ViewPager;
import com.fcwy.puzzle.R;
import com.fcwy.puzzle.adapter.UrlPagerAdapter;
/**
 * 以pop方式弹出图片预览
 * @author lanyj
 *
 */
public class PopupWindowBrowse extends PopupWindow {

	private View mPopView;
	private GalleryViewPager mViewPager;
	private TextView tv_name;
	private CheckBox photo_cbx;
	private int size=0;
	public PopupWindowBrowse(final Context context, List<String> imageUrl, int position,Bitmap bitmap,final CheckboxClickListener checkboxClickListener) {
		super(context);
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mPopView = inflater.inflate(R.layout.pop_browse_viewpage, null);
		mViewPager = (GalleryViewPager)mPopView.findViewById(R.id.browseViewer);
		tv_name=(TextView)mPopView.findViewById(R.id.tv_name);
		photo_cbx=(CheckBox) mPopView.findViewById(R.id.photo_cbx);
		size=imageUrl.size();
		tv_name.setText((position+1)+"/"+size);
		// 设置按钮监听
		this.setContentView(mPopView);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.MATCH_PARENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		// 点击外面的控件也可以使得PopUpWindow dimiss
		this.setOutsideTouchable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.BrowseStyle);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0xb0000000);//0xb0000000
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);//半透明颜色
		//this.setBackgroundDrawable(new ColorDrawable(0));//完全透明颜色
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框

		this.setTouchInterceptor(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {

				switch (event.getAction() & MotionEvent.ACTION_MASK) {
				
				case MotionEvent.ACTION_DOWN:
					//System.out.println("ACTION_DOWN");
					break;
				case MotionEvent.ACTION_POINTER_UP:
					//System.out.println("ACTION_POINTER_UP");
					//return false;
					break;
				case MotionEvent.ACTION_UP:
					if(mViewPager.mCurrentView==null) break;
					mViewPager.mCurrentView.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							dismiss();
						}
					});
					break;
				}
				return false;
			}
		});
	
      	if(imageUrl != null && imageUrl.size() > 0){
            //Collections.addAll(items, images);
            UrlPagerAdapter pagerAdapter = new UrlPagerAdapter(context, imageUrl,bitmap,null);
            
            //mViewPager.setOffscreenPageLimit(3);
            mViewPager.setAdapter(pagerAdapter);
            //mViewPager.setCurrentItem(position);
      	}
      	mViewPager.setOnPageChangeListener(new OnPageViewChangeListener());
      	mViewPager.setCurrentItem(position);
      	if(checkboxClickListener!=null) {
      		photo_cbx.setVisibility(View.VISIBLE);
      	}
      	photo_cbx.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
				if(checkboxClickListener!=null)
				checkboxClickListener.onConfirm(mViewPager.getCurrentItem());
			}
		});
	}
	public interface CheckboxClickListener {

		public void onConfirm(int position);//选择		
	}
	private class OnPageViewChangeListener implements ViewPager.OnPageChangeListener {
		@Override
		public void onPageSelected(int position) {
			//System.out.println("onPageSelected");
			//position = arg0;
			tv_name.setText((position+1)+"/"+size);
		}
		
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
		//	System.out.println("onPageScrolled");
			
		}
		
		@Override
		public void onPageScrollStateChanged(int position) {
			// TODO Auto-generated method stub
			//System.out.println("onPageScrollStateChanged");
			
		}
	}
}
